package com.mifengs.mall.ui.guide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.R;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.mall.ui.main.MainActivity;
import com.mifengs.mall.util.ToIntentUtil;
import com.mifengs.mall.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SplashActivity extends BaseActivity {

    private final int START_MAIN = 1;
    private final int START_GUIDE = 2;
    @BindView(R.id.iv_splash)
    ImageView mIvSplash;
    @BindView(R.id.bt_skip)
    TextView mBtSkip;
    private Dialog mNotificationDialog;
    private MyTime mMyTime;
    private String mRoute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        TinkerPatch.with().fetchPatchUpdate(true);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        PreferenceUtils.saveContacts(getContext(), "100");
        if (!Tools.isNotificationEnabled(getContext())) {
            showNotificationDialog();
        } else {
            init();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Tools.isNotificationEnabled(getContext())) {
            if (mNotificationDialog != null) {
                mNotificationDialog.dismiss();
                init();
            }
        }
    }

    private void init() {
        String url = PreferenceUtils.getSplashImg(getContext());
        String status = PreferenceUtils.getStringPreference(getContext(), PreferenceUtils.LocalUser.SPLASH_STATUS);
        String type = PreferenceUtils.getStringPreference(getContext(), PreferenceUtils.LocalUser.SPLASH_TYPE);
        mRoute = PreferenceUtils.getStringPreference(getContext(), PreferenceUtils.LocalUser.SPLASH_ROUTE);
        int time = 3000;
        if (status.equals("1")) { //启用
            try {
                time = Integer.parseInt(PreferenceUtils.getStringPreference(getContext(), PreferenceUtils.LocalUser.SPLASH_TIME)) * 1000;
            } catch (Exception e) {
                e.printStackTrace();
            }
            mMyTime = new MyTime(time, 1000);
            mMyTime.start();
        } else {
            mMyTime = new MyTime(time, 1000);
            mMyTime.start();
            return;
        }
        if (type.equals("0") || type.equals("1")) {
            if (!TextUtils.isEmpty(url)) {
                Glide.with(getContext())
                        .load(url)
                        .diskCacheStrategy(DiskCacheStrategy.RESULT)
                        .into(mIvSplash);
            }
        }
    }

    private void showNotificationDialog() {
        mNotificationDialog = new Dialog(this, R.style.dialog);
        mNotificationDialog.setContentView(R.layout.dialog_update);
        mNotificationDialog.show();
        TextView tvTile = (TextView) mNotificationDialog.findViewById(R.id.textView1);
        tvTile.setText("通知打开提醒");
        TextView tvContent = (TextView) mNotificationDialog.findViewById(R.id.tv_update_info);
        tvContent.setText("检查到当前的应用没有打开通知功能，无法正常收到推送消息，是否进行设置？");
        Button okBtn = mNotificationDialog.findViewById(R.id.bt_update);
        okBtn.setText("去设置");
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToSet();

            }
        });
        Button cancleBtn = mNotificationDialog.findViewById(R.id.bt_cancle);
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNotificationDialog.dismiss();
                init();

            }
        });
    }

    private void goToSet() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) {
            // 进入设置系统应用权限界面
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
            return;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 运行系统在5.x环境使用
            // 进入设置系统应用权限界面
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
            return;
        }
    }


    @OnClick({R.id.iv_splash, R.id.bt_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_splash:
               goToRouteActivity();
                break;
            case R.id.bt_skip:
                goToNextActivity();
                break;
        }
    }


    class MyTime extends CountDownTimer {

        public MyTime(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mBtSkip != null)
                mBtSkip.setText("跳过" + "(" + millisUntilFinished / 1000 + "s" + ")");
        }

        @Override
        public void onFinish() {
            goToNextActivity();
        }
    }


    private void goToNextActivity() {
        if (PreferenceUtils.getIsFirst(SplashActivity.this)) {
            PreferenceUtils.saveIsFirst(SplashActivity.this, false);
            Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


    private void goToRouteActivity(){
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        if(!TextUtils.isEmpty(mRoute)){
            ToIntentUtil.toCommonIntent(mRoute,getContext());
        }
        finish();
    }


    @Override
    protected void onDestroy() {
        if (mMyTime != null) {
            mMyTime.cancel();
        }
        super.onDestroy();
    }
}
